<template>
  <div id="login">
    <h2 class="title">鱼图图云图库---登录</h2>
    <div class="desc">企业级智能协同云图库</div>
    <a-form :model="formState" autocomplete="off" @finish="handleSubmit" class="form">
      <a-form-item name="username" :rules="[{ required: true, message: '请输入用户名' }]">
        <a-input v-model:value="formState.username" placeholder="用户名" />
      </a-form-item>

      <a-form-item name="password" :rules="[{ required: true, message: '请输入密码' }]">
        <a-input-password v-model:value="formState.password" placeholder="密码" />
      </a-form-item>

      <a-form-item>
        <div class="tips">
          没有账号？
          <router-link to="/user/regist">去注册</router-link>
        </div>
        <a-button type="primary" html-type="submit" style="width: 100%">登录</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { loginUsingPost } from '@/api/userController.ts'
import { useLoginUserState } from '@/stores/useLoginUserState.ts'
import { message } from 'ant-design-vue'
import { useRouter } from 'vue-router'

const formState = reactive<API.UserLoginReq>({
  username: '',
  password: '',
})
const userState = useLoginUserState()
const router = useRouter()
/**
 * 提交登录
 */
const handleSubmit = async (values: API.UserLoginReq) => {
  const res = await loginUsingPost(values)
  userState.setLoginUser(res.data)
  message.success('登录成功')
  router.replace('/')
}
</script>

<style scoped>
#login {
  max-width: 360px;
  margin: 0 auto;
}

.title {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 16px;
  text-align: center;
}

.desc {
  font-size: 14px;
  color: #999;
  margin-bottom: 16px;
  text-align: center;
}

.tips {
  color: #999999;
  text-align: right;
  margin-bottom: 10px;
}

.form {
  width: 100%;
}
</style>
